package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/16 23:18
 **/
public class Day31_7_Num60_GetPermutation {
    public static void main(String[] args) {
        System.out.println(getPermutation(3,3));
    }

    // 暴力回溯
    public static String res = "";
    public static int count = 0;
    public static String getPermutation(int n, int k) {
        int[] flag = new int[n+1];
        StringBuffer sb = new StringBuffer();
        dfs(n, k, sb, flag);
        return res;
    }
    private static void dfs(int n, int k,StringBuffer sb, int[] flag){
        if(count == k) return;
        if(sb.length() == n){
            count++;
            if(count == k){
                res += sb.toString();
                return;
            }
            return;
        }
        for(int i = 1;i <= n;++i){
            if(flag[i] == 1) continue;
            sb.append(i);flag[i] = 1;
            dfs(n,k,sb,flag);
            sb.deleteCharAt(sb.length()-1);flag[i] = 0;
        }
    }
}